package com.myth.文件转换;

import org.junit.Test;
import sun.nio.cs.ext.GBK;

import java.io.*;

public class 文件转换 {
    @Test
    public void 读取文件(){
        File 文件夹 = new File("D:\\documents\\QQ消息\\835478336\\FileRecv\\CV和LSV");
        File[] files = 文件夹.listFiles();
        for (int i = 0; i < files.length; i++) {
            File 目标文件 = files[i];
            String absolutePath = 目标文件.getAbsolutePath();
            String outputString = absolutePath.substring(0,absolutePath.length()-3) + "txt";
            File outputFile = new File(outputString);
            转化文件格式(目标文件,outputFile);
        }
    }

    public void 转化文件格式(File inputFile,File outputFile){
        FileInputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        FileOutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        try{
            inputStream = new FileInputStream(inputFile);
            inputStreamReader = new InputStreamReader(inputStream);

            outputStream = new FileOutputStream(outputFile);
            outputStreamWriter = new OutputStreamWriter(outputStream);
            byte[] buffer = new byte[1024];
//            char[] buffer = new char[1024];
            int len,sum = 0;
            byte[] aaa = new byte[0];
            while((len = inputStream.read(buffer)) != -1){

                byte[] bbb = java.util.Arrays.copyOf(aaa, aaa.length+len);;
                aaa = bbb;
                System.arraycopy(buffer,0,aaa,aaa.length-len,len);
                outputStream.write(buffer,0,len);
                sum += len;
            }
            System.out.println(sum);
//            System.out.println(new String(buffer));
            System.out.println(new String(aaa,"UTF-8"));
        } catch (IOException e){
            System.out.println(e.getMessage());
        } finally {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (outputStream != null)
                    outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
